package com.zyy.game.tianlong.handler;

import com.zyy.game.tianlong.codes.AccountCode;
import com.zyy.game.tianlong.codes.OpCode;
import com.zyy.game.tianlong.dto.account.AccountDto;
import com.zyy.game.tianlong.dto.account.WxLoginDto;
import com.zyy.game.tianlong.entity.Player;
import com.zyy.game.tianlong.network.GameContext;
import com.zyy.game.tianlong.network.GameManage;
import com.zyy.game.tianlong.network.annotation.GameEvent;
import com.zyy.game.tianlong.network.annotation.GameModule;
import com.zyy.game.tianlong.network.annotation.GameResult;
import com.zyy.game.tianlong.serviceimpl.AccountServiceImpl;
import com.zyy.game.tianlong.vo.PlayerInfo;
import com.zyy.game.tianlong.vo.ResultVo;
import org.springframework.stereotype.Component;

/**
 * 注册登录模块
 *
 * @author relufi
 * @date 2019/7/24 0024 08:56
 */
@Component
@GameModule(OpCode.ACCOUNT)
public class AccountGameHandler {
    private final AccountServiceImpl accountService;
    private final GameManage gameManage;

    public AccountGameHandler(AccountServiceImpl accountService, GameManage gameManage) {
        this.accountService = accountService;
        this.gameManage = gameManage;
    }


    /**
     * 手机号注册
     *
     * @param accountDto 登录的传输数据
     */
    @GameEvent(AccountCode.SIGN_UP_PHONE)
    @GameResult(defaultSuccess = true)
    public void signUpPhone(AccountDto accountDto) {
        accountService.addPhonePlayer(accountDto.getPassWord(), accountDto.getPhone(), accountDto.getPlayerName(), accountDto.getAuthCode());
    }

    /**
     * 微信登录与注册
     *
     * @param wxLoginDto 微信
     * @param ctx        game
     */
    @GameEvent(AccountCode.LOGIN_IN_WX)
    public void loginInWx(WxLoginDto wxLoginDto, GameContext ctx) {
        Player player = accountService.wxLogin(wxLoginDto.getOpenId(), wxLoginDto.getName(), wxLoginDto.getPhoto());
        ctx.writeAndFlush(OpCode.ACCOUNT, AccountCode.LOGIN_IN_WX, ResultVo.success(PlayerInfo.convertPlayer(player)));
        gameManage.put(player, ctx);
    }

    /**
     * 密码登录
     *
     * @param accountDto 登录的传输数据
     * @param ctx        game
     */
    @GameEvent(AccountCode.LOGIN_IN_PW)
    @GameResult
    public void loginInPw(AccountDto accountDto, GameContext ctx) {
        Player player = accountService.passWordLogin(accountDto.getPhone(), accountDto.getPassWord());
        ctx.writeAndFlush(OpCode.ACCOUNT, AccountCode.LOGIN_IN_PW, ResultVo.success(PlayerInfo.convertPlayer(player)));
        gameManage.put(player, ctx);
    }

    /**
     * 手机短信验证登录
     *
     * @param accountDto 登录的传输数据
     * @param ctx        game
     */
    @GameEvent(AccountCode.LOGIN_IN_PHONE)
    public void loginInPhone(AccountDto accountDto, GameContext ctx) {
        Player player = accountService.phoneLogin(accountDto.getPhone(), accountDto.getAuthCode());
        ctx.writeAndFlush(OpCode.ACCOUNT, AccountCode.LOGIN_IN_PHONE, ResultVo.success(PlayerInfo.convertPlayer(player)));
        gameManage.put(player, ctx);
    }

    /**
     * 查看用户名是否已存在
     *
     * @param playerName 用户名
     * @param ctx      game
     */
    @GameEvent(AccountCode.EXIST_NAME)
    public void exitsName(String playerName, GameContext ctx) {
        ctx.writeAndFlush(OpCode.ACCOUNT, AccountCode.EXIST_NAME, ResultVo.success(accountService.isExistPlayerName(playerName)));
    }

    /**
     * 获取登录手机短信验证码
     *
     * @param phone 手机号
     */
    @GameEvent(AccountCode.LOGIN_IN_PHONE_CODE)
    @GameResult(defaultSuccess = true)
    public void loginInPhoneCode(String phone) {
        accountService.phoneCodeLoginIn(phone);
    }

    /**
     * 获取注册手机短信验证码
     *
     * @param phone 手机号
     */
    @GameEvent(AccountCode.SIGN_UP_PHONE_CODE)
    @GameResult(defaultSuccess = true)
    public void signInPhoneCode(String phone) {
        accountService.phoneCodeSignUp(phone);
    }

    /**
     * 退出登录
     *
     * @param ctx game
     */
    @GameEvent(AccountCode.LOGOUT)
    public void logOut(GameContext ctx) {
        gameManage.remove(ctx);
    }

}
